<?php

namespace App\Trip\Passengers;

use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class Passengers
{
    const PASSENGER_TYPE_ADULT    = 1;
    const PASSENGER_TYPE_STUDENT  = 2;
    const PASSENGER_TYPE_CHILD    = 3;
    const PASSENGER_TYPE_SOLDIER  = 4;
    const PASSENGER_TYPE_CRIPPLED = 5;
    const PASSENGER_TYPE_CHILD2   = 13;

    /**
     * 身份证
     */
    const IDENTITY_TYPE_ID_CARD = 1;
    /**
     * 护照
     */
    const IDENTITY_TYPE_PASSPORT = 2;
    /**
     * 名字中的特殊字符
     */
    const ALLOW_SPECIAL_STRING = '·';
    const SPECIAL_STRINGS
                               = [
            '.', '?', '/',
            '\\', ',', '，', '。',
            '、', '【', '】',
            '[', ']', '(', ')',
            '+', '-', '*', '&',
            '%', '$', '#', '@',
            '!', '！', '~', '《',
            '》', '<', '>', '‘',
            '’', ' ', '`',
        ];
    /**
     * 乘客类型：儿童/成人/学生/残军
     */
    const PASSENGER_TYPE_DESC
        = [
            self::PASSENGER_TYPE_ADULT    => "成人票",
            self::PASSENGER_TYPE_STUDENT  => "学生票",
            self::PASSENGER_TYPE_CHILD    => "儿童票",
            self::PASSENGER_TYPE_SOLDIER  => "残军票",
            self::PASSENGER_TYPE_CRIPPLED => "残军票",
            self::PASSENGER_TYPE_CHILD2   => "儿童票",
        ];


    /**
     *  证件类型：身份证、港澳通行证、台湾通行证、护照、外国人永久居留身份证、港澳台居民居住证",
     */
    const IDENTITY_TYPE_DESC
        = [
            self::IDENTITY_TYPE_ID_CARD  => "身份证",
            self::IDENTITY_TYPE_PASSPORT => "护照",
        ];

    static function ChildrenTypes(): array
    {
        return [
            self::PASSENGER_TYPE_CHILD,
            self::PASSENGER_TYPE_CHILD2,
        ];
    }

    /**
     * @throws Exception
     */
    static function dealTrueName(array $data): string
    {
        if (empty($data['truename']) && empty($data['first_name']) && empty($data['last_name'])) {
            throw new Exception('请填写乘客姓名');
        }
        if (!empty($data['last_name'])) {
            if (isset($data['first_name'])) {
                $data['first_name'] = self::filterSidesAllowSpecialString($data['first_name']);
                self::validName($data['first_name']);
            }
            $data['last_name'] = self::filterSidesAllowSpecialString($data['last_name']);
            self::validName($data['last_name']);
            $data['first_name'] = self::filterSpecialString($data['first_name'] ?? '');
            $data['last_name']  = self::filterSpecialString($data['last_name']);
            $truename           = $data['first_name'] ? $data['last_name'].self::ALLOW_SPECIAL_STRING.$data['first_name'] : $data['last_name'];
        } else {
            $truename = $data['truename'];
            self::validName($truename);
        }

        return self::filterSpecialString($truename);
    }

    private static function filterSidesAllowSpecialString(string $string): string
    {
        // 除去两边的 特殊符号
        return trim($string, self::ALLOW_SPECIAL_STRING);

    }

    /**
     * @throws Exception
     */
    public static function validName(string $name): void
    {
        if (Str::contains($name, self::ALLOW_SPECIAL_STRING)) {
            if (Str::startsWith($name, self::ALLOW_SPECIAL_STRING) || Str::endsWith($name, self::ALLOW_SPECIAL_STRING)) {
                Log::error("Invalid1 name :{$name}");
                throw new Exception("不允许特殊字符");
            }
            return;
        }
        $regex = '/[\p{P}A-Za-z0-9]+/u';
        preg_match($regex, $name, $matches);
        if (!empty($matches)) {
            throw new Exception("不允许特殊字符");
        }
    }

    private static function filterSpecialString(string $string): string
    {
        if (!$string) {
            return $string;
        }

        return trim(Str::replace(self::SPECIAL_STRINGS, "", $string));
    }
}